package com.cmrx.model.service.impl;

import com.cmrx.model.dao.*;
import com.cmrx.model.entity.*;
import com.cmrx.model.service.CommonPictureServer;
import com.cmrx.model.service.SceneFootPrintService;
import com.cmrx.model.service.SceneFootPrintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class SceneFootPrintServiceImpl implements SceneFootPrintService {
    @Autowired
    private SceneFootPrintDao sceneFootPrintDao;

    @Autowired
    private CommonPictureDao commonPictureDao;

    @Autowired
    private PictureThumbnailDao pictureThumbnailDao;

    @Autowired
    private CommonPictureServer commonPictureServer;

    //根据现勘id获取足迹物证列表信息
    @Override
    public List<SceneFootPrint> queryByInvestigationId(String investigationId) {
        List<SceneFootPrint> foots = sceneFootPrintDao.queryByInvestigationId(investigationId);
        List<SceneFootPrint> list = new ArrayList<>();
        for(SceneFootPrint foot:foots) {
            CommonPictureThumbnail com = pictureThumbnailDao.queryByPictureId(foot.getFootprintPhotoId());
            foot.setContent(com.getContent());
            list.add(foot);
        }
        return list;
    }

    //根据现勘id获取足迹物证统计
    @Override
    public Integer queryCount(String investigationId) {
        return sceneFootPrintDao.queryCount(investigationId);
    }

    //根据id查找足迹物证信息
    @Override
    public SceneFootPrint queryById(String id) {
        SceneFootPrint foot = sceneFootPrintDao.queryById(id);
        if(foot!=null) {
            CommonPicture com = commonPictureDao.queryById(foot.getFootprintPhotoId());
            foot.setContent(com.getContent());
        }
        return foot;
    }

    //添加足迹物证信息
    @Override
    public String addFootPrint(SceneFootPrint footPrint, User user,CommonPicture commonPicture) {
        commonPicture.setInvestigationId(footPrint.getInvestigationId());
        commonPicture.setContent(footPrint.getContent());
        commonPicture.setCategory("4");
        String pictureId = commonPictureServer.AddPicture(user,commonPicture);
        int serialNo = 1;
        Integer no = sceneFootPrintDao.queryMaxNo(footPrint.getInvestigationId());
        if(no!=null){
            serialNo = no+1;
        }
        footPrint.setCreateUserId(user.getUserid());
        footPrint.setCreateUser(user.getUserName());
        footPrint.setFootprintPhotoId(pictureId);
        footPrint.setId(UUID.randomUUID().toString().replaceAll("-",""));
        footPrint.setSerialNo(serialNo+"");
        int count = sceneFootPrintDao.addFootPrint(footPrint);
        if(count>0) {
            return footPrint.getId();
        }else{
            return "";
        }
    }

    //根据id修改足迹物证信息
    @Override
    public int updateById(SceneFootPrint footPrint,User user) {
        SceneFootPrint foot = sceneFootPrintDao.queryById(footPrint.getId());
        commonPictureServer.updatePicture(user,foot.getFootprintPhotoId(),footPrint.getContent());
        footPrint.setUpdateUser(user.getUserName());
        int count = sceneFootPrintDao.updateById(footPrint);
        return count;
    }

    //根据id删除足迹物证信息
    @Override
    public int deleteById(String id) {
        SceneFootPrint foot = sceneFootPrintDao.queryById(id);
        if(foot!=null) {
            pictureThumbnailDao.deleteById(foot.getFootprintPhotoId());
            commonPictureDao.deleteById(foot.getFootprintPhotoId());
        }
        int result = sceneFootPrintDao.deleteById(id);
        return result;
    }
}
